import {createRouter,createWebHistory} from 'vue-router'

import IndexVue from '@/views/index.vue'
import guideVue from '@/views/guide.vue'
import registerVue_1 from '@/views/teacher/register_1.vue'
import registerVue_2 from '@/views/teacher/register_2.vue'
import judgesVue from '@/views/judges/judges.vue'
import winListVue from '@/views/winList.vue'
import worksListVue from '@/views/worksList.vue'
import Layout from '@/views/Layout.vue'
import scoreVue from '@/views/judges/score.vue'
import {useUserStore} from "@/store/modlues/user/user.js";
import {ElMessage} from "element-plus";
const routes=[
    {
        path: '/',
        redirect:'/index',
        component: Layout,
        children: [
            {
                path: '/index',
                component: IndexVue
            },
            {
                path: '/guide',
                component: guideVue
            },
            {
                path: '/register_1',
                component: registerVue_1
            },
            {
                path: '/register_2',
                component: registerVue_2
            },
            {
                path: '/judges',
                component: judgesVue
            },
            {
                path: '/winList',
                component: winListVue
            },
            {
                path: '/worksList',
                component: worksListVue
            },
            {
                path:'/score',
                component:scoreVue
            }
        ]
    }

]


const router=createRouter({
    history: createWebHistory(),
    routes: routes
})
const authUrl = ['/score','/judges','/register_2','/register_1']
    router.beforeEach((to, from, next) => {
        const userStore=useUserStore();
        if (!authUrl.includes(to.path)) {
            next()
        }
        if(userStore.token===''&&authUrl.includes(to.path)){
            next('/')
            ElMessage.warning("登录超时,请重新登录")
        }else {
            next()
        }
    });
export default router